package top.szhome.framework.starter.db.support;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import top.szhome.framework.core.tools.support.BaseConverter;
import top.szhome.framework.core.tools.support.Pagination;
import top.szhome.framework.core.tools.support.Resp;
import top.szhome.framework.core.tools.validator.Add;
import top.szhome.framework.core.tools.validator.Update;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 简单接口实现
 *
 * @author 唐加利
 * @date 2023/7/27
 **/
public abstract class SimpleController<S extends SimpleService<C, D, T, V>, C extends BaseConverter<D, T, V>, Q, D, T extends BaseEntity, V> {

    /**
     * 基础服务
     */
    @Autowired
    private S baseService;

    /**
     * 获取基础服务
     * @return
     */
    public S getBaseService() {
        return baseService;
    }

    /**
     * 新增实体对象
     *
     * @param req 实体数据载体
     * @return 返回数据实体新增成功后生成的ID
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "新增", description = "新增一条数据实体")
    @PostMapping("add")
    public Resp<Long> add(@RequestBody @Validated(Add.class) D req) {
        return Resp.success(baseService.insert(req));
    }

    /**
     * 更新实体数据
     *
     * @param req   实体数据载体
     * @return 返回更新结果
     */
    @ApiOperationSupport(order = 2)
    @Operation(summary = "更新", description = "更新数据实体信息")
    @PostMapping("update")
    public Resp<Boolean> update(@RequestBody @Validated(Update.class) D req) {
        return Resp.success(getBaseService().update(req));
    }

    /**
     * 删除数据实体
     *
     * @param id 数据实体ID
     * @return  删除成功时返回true，反之返回false
     */
    @ApiOperationSupport(order = 3)
    @Operation(summary = "删除", description = "删除指定数据")
    @PostMapping("delete")
    public Resp<Boolean> delete(@RequestBody @Parameter(example = "9527,3561") @NotNull(message = "ID不能为空") List<Long> id) {
        return Resp.success(getBaseService().delete(id));
    }

    /**
     * 查询单个数据实体
     *
     * @param id 数据实体ID
     * @return  返回指定ID的数据实体详细信息
     */
    @ApiOperationSupport(order = 4)
    @Operation(summary = "单个查询", description = "根据ID获取单个数据实体")
    @GetMapping(value = "get")
    public Resp<V> get(@Parameter(example = "9527") @NotNull(message = "ID不能为空") Long id){
        return Resp.success(getBaseService().get(id));
    }

    /**
     * 分页查询数据实体
     *
     * @param query 查询条件
     * @param current  请求页码
     * @param pageSize  单页数据条数
     * @return 返回符合条件的数据实体列表
     */
    @ApiOperationSupport(order = 5)
    @Operation(summary = "分页查询", description = "查询符合条件的数据实体，默认单页10条数据")
    @Parameter(name = "current", description = "请求页码，默认为1", example = "3")
    @Parameter(name = "pageSize", description = "单页容量，默认为10", example = "10")
    @GetMapping("page")
    public Pagination<V> page(@Validated @ParameterObject Q query, @RequestParam(required = false, defaultValue = "1") long current, @RequestParam(required = false, defaultValue = "10") long pageSize) {
        return getBaseService().page(query, new Page(current, pageSize));
    }

    /**
     * 列表查询实体数据
     * @param query
     * @return
     */
    @ApiOperationSupport(order = 6)
    @Operation(summary = "列表查询", description = "查询符合条件的数据实体数据列表")
    @GetMapping("list")
    public Resp<List<V>> list(@Validated @ParameterObject Q query) {
        return Resp.success(getBaseService().list(query));
    }

}
